const FormatDate = (date, fmt) => {
  // 对Date的扩展，将 Date 转化为指定格式的String
  // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
  // 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
  // 例子：
  // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
  // (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
  // author: meizz
  var o = {
    "M+": date.getMonth() + 1, // 月份
    "d+": date.getDate(), // 日
    "h+": date.getHours(), // 小时
    "m+": date.getMinutes(), // 分
    "s+": date.getSeconds(), // 秒
    "q+": Math.floor((date.getMonth() + 3) / 3), // 季度
    S: date.getMilliseconds() // 毫秒
  };
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(
      RegExp.$1,
      (date.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
  }
  for (var k in o) {
    if (new RegExp("(" + k + ")").test(fmt)) {
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
      );
    }
  }
  return fmt;
};
export function getCompressProductImg(path, size) {
  if (isReallyNull(path)) {
    path = "../images/icon/noproductimg.png";
    return path;
  }
  var postion = path.lastIndexOf("/");
  return (
    path.substring(0, postion + 1) + size + path.substring(postion, path.length)
  );
}
export function getCurrentTime() {
  let keep = "";
  let date = new Date();
  let y = date.getFullYear();
  let m = date.getMonth() + 1;
  m = m < 10 ? "0" + m : m;
  let d = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  let h = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  let f = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  let s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  keep = y + "" + m + "" + d + "" + h + "" + f + "" + s;
  return keep; // 20160614134947
}
export function getuniqueID() {
  return Number(
    Math.random()
      .toString()
      .substr(3, length) + Date.now()
  ).toString(36);
}
export function getReallyLength(str) {
  var length = 0;
  if (typeof str === "string") {
    var tempstr = str.replace(/([\u4e00-\u9fa5])/g, "aa");
    length = tempstr.length;
  }
  return length;
}

export function subStr(str, num, endstr = "") {
  var tempstr = null;
  if (typeof str === "string") {
    var strlength = getReallyLength(str);
    if (typeof num === "number") {
      if (strlength <= num) {
        tempstr = str;
      } else {
        var reg = /[\u4e00-\u9fa5]/;
        var tempnum = 0;
        tempstr = "";
        for (const s of str) {
          ++tempnum;
          if (reg.test(s)) {
            ++tempnum;
          }
          if (tempnum <= num) {
            tempstr += s;
          }
        }
      }
    }
  }
  tempstr = tempstr + endstr;
  return tempstr;
}
export function objLength(input) {
  let type = toString(input);
  let length = 0;
  if (type !== "[object Object]") {
    // throw '输入必须为对象{}！'
  } else {
    for (let key in input) {
      if (key !== "number") {
        length++;
      }
    }
  }
  return length;
}
// 验证是否是手机号码
export function vailPhone(number) {
  let flag = true;
  let myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(17[0]{1})|(15[0-3]{1})|(15[5-9]{1})|(18[0-9]{1}))+\d{8})$/;
  if (number.length !== 11 || !myreg.test(number)) {
    flag = false;
  }
  return flag;
}
// 验证是否西班牙手机(6开头 9位数)
export function ifSpanish(number) {
  let flag = true;
  let myreg = /^([6|7|9]{1}(\d+){8})$/;
  if (number.length !== 9 || !myreg.test(number)) {
    flag = false;
  }
  return flag;
}
// 浮点型除法
export function div(a, b) {
  let c, d, e, f;
  try {
    e = a.toString().split(".")[1].length;
  } catch (g) {}
  try {
    f = b.toString().split(".")[1].length;
  } catch (g) {}
  // [eslint] Return statement should not contain assignment. (no-return-assign)
  c = Number(a.toString().replace(".", ""));
  d = Number(b.toString().replace(".", ""));
  return mul(c / d, Math.pow(10, f - e));
}
// 浮点型加法函数
export function accAdd(arg1, arg2) {
  let r1, r2, m;
  try {
    r1 = arg1.toString().split(".")[1].length;
  } catch (e) {
    r1 = 0;
  }
  try {
    r2 = arg2.toString().split(".")[1].length;
  } catch (e) {
    r2 = 0;
  }
  m = Math.pow(10, Math.max(r1, r2));
  return ((arg1 * m + arg2 * m) / m).toFixed(2);
}
// 浮点型乘法
export function mul(a, b) {
  let c = 0;
  let d = a.toString();
  let e = b.toString();
  try {
    c += a.toString().split(".")[1].length;
  } catch (f) {}
  try {
    c += b.toString().split(".")[1].length;
  } catch (f) {}
  return (
    (Number(d.replace(".", "")) * Number(e.replace(".", ""))) / Math.pow(10, c)
  );
}

//  遍历对象属性和值
export function displayProp(obj) {
  let names = "";
  for (let name in obj) {
    names += name + obj[name];
  }
  return names;
}
//  去除字符串所有空格
export function sTrim(text) {
  return text.replace(/\s/g, "");
}
// 去除所有:,英文冒号
export function replaceColon(txt) {
  return txt.replace(/:/g, "");
}
// 转换星星分数
export function convertStarArray(score) {
  // 1 全星,0 空星,2半星
  let arr = [];
  for (let i = 1; i <= 5; i++) {
    if (score >= i) {
      arr.push(1);
    } else if (score > i - 1 && score < i + 1) {
      arr.push(2);
    } else {
      arr.push(0);
    }
  }
  return arr;
}

/**
 * 对象排序：使用方法 Array.sort(sortObj("key"))
 * @param {String} compareKye 比较对象中比较属性
 * @param {String} orderby 排序方式：asc 升序，desc 降序
 */
export function sortObj(compareKye, orderby = "asc") {
  return function(obja, objb) {
    let a = obja[compareKye];
    let b = objb[compareKye];
    if (orderby.toLowerCase() === "desc") {
      if (a < b) return 1;
      else if (a > b) return -1;
      else return 0;
    } else {
      if (a < b) return -1;
      else if (a > b) return 1;
      else return 0;
    }
  };
}
/**
 * 是null或者是""或者是{}或是undefined
 * @param {Object} obj
 */
export function isReallyNull(obj) {
  let isReallyNull = false;
  if (typeof obj === "undefined") {
    isReallyNull = true;
  }
  if (typeof obj === "string") {
    if (obj.trim().length === 0) {
      isReallyNull = true;
    }
  }
  if (typeof obj === "object") {
    if (obj === null) {
      isReallyNull = true;
    } else {
      isReallyNull = Object.keys(obj).length === 0;
    }
  }
  return isReallyNull;
}

export function getVideoFirstPicture(obj) {
  var fp = "";
  fp = obj.substring(0, obj.lastIndexOf(".") + 1) + "jpg";
  return fp;
}

/**
 *
 * @param {Array} tempArray  求和的数组
 * @param {String} key 数组item中的要求和的item.key
 * @returns {Number} 和
 */
export function arraySum(tempArray, key = "") {
  let totalsum = 0;
  let isobj = false;
  if (key.trim().length > 0) {
    isobj = true;
  }
  if (tempArray instanceof Array) {
    for (let index = 0; index < tempArray.length; index++) {
      let element;
      if (isobj) {
        element = tempArray[index][key];
      } else {
        element = tempArray[index];
      }
      totalsum += element;
    }
  }
  return totalsum;
}

/**
 * 拼接对象为请求字符串
 * @param {Object} obj - 待拼接的对象
 * @returns {string} - 拼接成的请求字符串
 */
export function encodeSearchParams(obj) {
  const params = [];
  Object.keys(obj).forEach(key => {
    let value = obj[key];
    // 如果值为undefined我们将其置空
    if (typeof value === "undefined") {
      value = "";
    }
    // 对于需要编码的文本（比如说中文）我们要进行编码
    params.push([key, encodeURIComponent(value)].join("="));
  });

  return params.join("&");
}

var familyNames = [
  "赵",
  "钱",
  "孙",
  "李",
  "周",
  "吴",
  "郑",
  "王",
  "冯",
  "陈",
  "褚",
  "卫",
  "蒋",
  "沈",
  "韩",
  "杨",
  "朱",
  "秦",
  "尤",
  "许",
  "何",
  "吕",
  "施",
  "张",
  "孔",
  "曹",
  "严",
  "华",
  "金",
  "魏",
  "陶",
  "姜",
  "戚",
  "谢",
  "邹",
  "喻",
  "柏",
  "水",
  "窦",
  "章",
  "云",
  "苏",
  "潘",
  "葛",
  "奚",
  "范",
  "彭",
  "郎",
  "鲁",
  "韦",
  "昌",
  "马",
  "苗",
  "凤",
  "花",
  "方",
  "俞",
  "任",
  "袁",
  "柳",
  "酆",
  "鲍",
  "史",
  "唐",
  "费",
  "廉",
  "岑",
  "薛",
  "雷",
  "贺",
  "倪",
  "汤",
  "滕",
  "殷",
  "罗",
  "毕",
  "郝",
  "邬",
  "安",
  "常",
  "乐",
  "于",
  "时",
  "傅",
  "皮",
  "卞",
  "齐",
  "康",
  "伍",
  "余",
  "元",
  "卜",
  "顾",
  "孟",
  "平",
  "黄",
  "和",
  "穆",
  "萧",
  "尹"
];

export function getfamilyName() {
  let familyName =
    familyNames[
      parseInt(10 * Math.random()) * 10 + parseInt(10 * Math.random())
    ];
  familyName += Math.round(Math.random() + 1) === 1 ? "*" : "**";
  return familyName;
}
